<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
if (!defined("Member_lib_include")){
	define("Member_lib_include", 1);
	include_once(__Modules_Path."/UserSessions.lib.php");
	include_once(__Modules_Path."/LogRecord.lib.php");

/**
 * 依據管理者帳號是否符合規定
 * @param	strAcc 管理者帳號
 * @return	false -> 錯誤，object -> 成功, 

	function isCorrectAccount($strAcc){
		global $_Conn, $$_Conn;

		if(!preg_match("/^([A-z0-9_\@\-])+$/i", $strAcc)){
			return false;
		}
		return true;
	}
 */
/**
 * 檢查次使用者是否屬於此修改者的底下子層
 * @param	intCUID 修改者序號
 * @param	intUID 使用者序號
 * @return	false -> 錯誤，0 -> 不是，1 -> 是
 */
	function isChildOfChangeUser($intCUID, $intUID){
		global $_Conn, $$_Conn;
		if($intCUID == 0 || $intUID == 0)return 0;

		$strSQL = "select ParentID from `bet_Member` where ID = ".$intUID;
		if(!$RS = PMA_mysql_query($strSQL, $$_Conn)){
			$GLOBALS["errormessage"] = "Find User Data Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($$_Conn).",\nLinue:".__LINE__.",\nPage:".__FILE__;
			return false;
		}

		if(PMA_mysql_num_rows($RS) == 0)return 0;
		$Rows = PMA_mysql_fetch_object($RS);
		if($intCUID == $Rows -> ParentID)return 1;
		return isChildOfChangeUser($intCUID, $Rows -> ParentID);
	}


/**
 * 當此父層資料修改後，及其和其子層踢出
 * @param	strMemID 使用者序號
 * @return	null
 */
	function getOutUser($strMemID){
		global $_Conn, $$_Conn;
		if(!UserSession_getOut($strMemID)){
			ErrMsgOutputHTML("LoginError", "", 1);
		}

		$strSQL = "select ID from `bet_Member` where ParentID in (".$strMemID.")";
		if(!$RS = @PMA_mysql_query($strSQL, $$_Conn)){
			$GLOBALS["errormessage"] = "Find Member Data Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($$_Conn).",\nLinue:".__LINE__.",\nPage:".__FILE__;
			ErrMsgOutputHTML("LoginError", "", 1);
		}

		if(@PMA_mysql_num_rows($RS) > 0){
			while($Rows = @PMA_mysql_fetch_object($RS)){
				$arrID[] = $Rows -> ID;
			}
			$strMemID = implode(",",$arrID);
			getOutUser($strMemID);
		}
		@PMA_mysql_free_result($RS);
	}  /*  function getOutUser()  */


/**
 * 產生各階層管理下拉清單
 * @param	intMemID 使用者序號
 * @param	intParentID 父層序號
 * @param	intGroup 群組
 * @param	strFieldName select tag 的name
 * @param	onChange onchange 時，要執行的事件
 * @param	intType 尋找類別
 * @return	成功傳回 String
 */
	function getManagerSelectTag($intUserID, $intParentID, $intGroup, $strFieldName, $onChange, $intType){
		global $_Conn, $$_Conn;
		$strSQL = "select ID, Account from `bet_Member` where MemberGroup = ".$intGroup." and isLock = '0' ";
		switch( $intType){
			case 5:
				$strSQL .= "and DirectorID = ".$intParentID;
				break;
			case 4:
				$strSQL .= "and StockID = ".$intParentID;
				break;
			case 3:
				$strSQL .= "and MainID = ".$intParentID;
				break;
			case 2:
				$strSQL .= "and SubID = ".$intParentID;
				break;
			case 0:
				break;
			default:
				break;
		}
		//print $strSQL;
		if(empty($onChange))$onChange="void(0)";
		$strCon = "<select name=\"".$strFieldName."\" onChange=\"".$onChange."\">\n";
		$strCon .= "<option value=\"\">--select--</option>\n";
		if($RS = @PMA_mysql_query($strSQL, $$_Conn)){
			while($Rows = PMA_mysql_fetch_object($RS)){
				$strCon .= "<option value=\"".$Rows -> ID."\"".(($intUserID == $Rows -> ID)?" selected":"").">".$Rows -> Account."</option>\n";
			}
		}
		$strCon .= "</select>";
		return $strCon;
	}


/**
 * 找出該使用者名稱和帳號
 * @param	intUID 使用者序號
 * @return	成功傳回 Array
 */
	function getUserAccount($intUID){
		global $_Conn, $$_Conn;
		$arrCon = array();
		$strSQL = "select Account from `bet_Member` where ID = ".$intUID;
		if(!$RS = PMA_mysql_query($strSQL, $$_Conn)){
			$GLOBALS["errormessage"] = "Find Member Data Error!!";
			$GLOBALS["errorhidden"] = "\n Error: ".PMA_mysql_error($$_Conn).",\nSQL: ".$strSQL.",\nLine: ".__LINE__.",\nPage: ".__FILE__;
			return false;
		}
		if(PMA_mysql_num_rows($RS) == 0)return "";
		$Rows = PMA_mysql_fetch_object($RS);
		return $Rows -> Account;
	}

/**
 * 取得分盤下拉選單
 * @param	intGLine 分盤序號
 * @param	strFieldName select tag 的name
 * @param	onChange onchange 時，要執行的事件
 * @return	成功傳回 String
 */
	function getGLineSelectTag($intGLine, $strFieldName, $onChange){
		global $_arrGLineTip;
		$strCon = "<select name=\"".$strFieldName."\" onChange=\"".$onChange."\">\n";
		$strCon .= "<option value=\"\">--select--</option>\n";
		if(is_array($_arrGLineTip)){
			foreach($_arrGLineTip as $intGL => $strGL){
				if($intGL == 0)continue;
				$strCon .= "<option value=\"".$intGL."\"".(($intGLine == $intGL)?" selected":"").">".$strGL."</option>\n";
			}
		}
		$strCon .= "</select>";
		return $strCon;
	}

/**
 * 找出該使用者底下人員數量
 * @param	intUserID 使用者序號
 * @param	intGroup 群組
 * @param	intType 尋找類別
 * @param	strLock 是否停用會員
 * @return	成功傳回 int
 */
	function getChildrenNo($intUserID, $intGroup, $intType, $strLock){
		global $_Conn, $$_Conn;
		switch($intType){
			case 0:
				$strSQL = "select COUNT(*) as intTotal from bet_Member where MemberGroup = ".$intGroup." and isLock in (".$strLock.")";
				break;

			case 1:
				$strSQL = "select COUNT(*) as intTotal from bet_Member where MemberGroup = ".$intGroup." and ParentID = ".$intUserID." and isLock in (".$strLock.")";
				break;

			case 2:
				$strSQL = "select COUNT(*) as intTotal from bet_Member a, bet_Member b where b.MemberGroup = ".$intGroup." and a.ParentID = ".$intUserID." and b.ParentID = a.ID and b.isLock in (".$strLock.")";
				break;

			case 3:
				$strSQL = "select COUNT(*) as intTotal from bet_Member a, bet_Member b, bet_Member c where c.MemberGroup = ".$intGroup." and b.ParentID = a.ID and c.ParentID = b.ID and a.ParentID =".$intUserID." and c.isLock in (".$strLock.")";
				break;
		}
		$intCon = 0;
		//print $strSQL;
		if($RS = @PMA_mysql_query($strSQL, $$_Conn)){
			$Rows = PMA_mysql_fetch_object($RS);
			$intCon = $Rows -> intTotal;
		}
		return $intCon;
	}

/**
 * 找出該使用者底下各階層人員數量
 * @param	intUserID 使用者序號
 * @param	intGroup 群組
 * @param	strLock 是否停用會員
 * @return	成功傳回 int
 */
	function getAllLayerChildrenNo($intUserID, $intGroup, $strLock){
		global $_Conn, $$_Conn;
		$arrGroup = array(2 => 'SubID', 3 => 'MainID', 4 => 'StockID', 5 => 'DirectorID');
		$strSQL = "select MemberGroup, COUNT(*) as intTotal from `bet_Member` where ".$arrGroup[$intGroup]." = ".$intUserID." and isLock in (".$strLock.") group by MemberGroup";
		$arrCon = array();
		//print $strSQL;
		if($RS = @PMA_mysql_query($strSQL, $$_Conn)){
			while($Rows = PMA_mysql_fetch_object($RS))	$arrCon[$Rows -> MemberGroup] = $Rows -> intTotal;
		}
		return $arrCon;
	}

/**
 * 產生會員匯率下拉清單
 * @param	intERID 已選擇匯率序號
 * @param	strFName select tag 的name
 * @param	strOnChange onchange 時，要執行的事件
 * @param	arrECRate 匯率參數陣列
 * @return	成功傳回 String
 */
	function getExchangeRateSelectTag($intERID, $strFName, $strOnChange, $arrECRate){
		global $_intLang;
		$strCon = "<select name=\"".$strFName."\" onChange=\"".$strOnChange."\">\n";
		$strCon .= "<option value=\"\">--select--</option>\n";
		while(list($k, $v) = each($arrECRate)){
			if($v["Rate"] == 0)continue; 
			$strCon .= "<option value=\"".$k."\"".(($intERID == $k)?" selected":"").">".$v[$_intLang]."</option>\n";
		}
		$strCon .= "</select>";
		return $strCon;
	}


/**
 * 取得使用者其下限使用者信用額總額
 * @param	intUserID 使用者序號
 * @param	strNotSumUID 不參與信用計算之使用者序號
 * @param	intUserStatus 使用者狀態
 * @return	成功傳回 int
 */
	function getChildSumCreditLine($intUserID, $strNotSumUID, $intUserStatus){
		global $_Conn, $$_Conn;
		$strSQL = "select SUM(CreditLine) as sumCreditLine from `bet_Member` where ParentID = '".(string)$intUserID."' and isLock = '".$intUserStatus."'";
		if(!empty($strNotSumUID))$strSQL .= " and ID not in (".$strNotSumUID.")";
		//print $strSQL;
		if(!$RS = @PMA_mysql_query($strSQL, $$_Conn)){
			$GLOBALS["errormessage"] = "Find Member Data Error!!";
			$GLOBALS["errorhidden"] = "\n Error: ".PMA_mysql_error($$_Conn).",\nSQL: ".$strSQL.",\nLine: ".__LINE__.",\nPage: ".__FILE__;
			return false;
		}

		if(PMA_mysql_num_rows($RS) == 0)return 0;
        $Rows = @PMA_mysql_fetch_object($RS);
		return $Rows -> sumCreditLine;
	}


/**
 * 更改使用者及其下階層信用額度
 * @param	intChangeUID 更改者序號
 * @param	intUID 使用者序號
 * @param	strChildUID 使用者子層序號
 * @param	intOldCredit 原信用額度
 * @param	intNewCredit 欲更改信用額度
 * @param	intUserStatus 使用者狀態
 * @return	成功 true, 失敗 false
 */
	function setUserAndChildCredit($intChangeUID, $intUID, $strChildUID, $intOldCredit, $intNewCredit, $intUserStatus){
		global $_ConnMaster;

		if($intNewCredit < 0 || $intOldCredit < 0){
			$GLOBALS["errormessage"] = "Credit Data Error!!";
			$GLOBALS["errorhidden"] = "\nNewCredit: ".$intNewCredit.",\nOldCredit:".$intOldCredit.",\nLinue:".__LINE__.",\nPage:".__FILE__;
			AddData_Log($intChangeUID, "更改使用者信用額度錯誤", $GLOBALS["errorhidden"]);
			return false;
		//與更改信用額度和原來一樣，則不更改
		}elseif($intNewCredit == $intOldCredit || $intOldCredit < 0){
			return true;
		}

		//更改信用額度紀錄
		$strSQL = "insert `bet_RateChangeRecord`(ChangeUserID, UserID, LogType, Scale, NewScale, ChangeDate, IP) values(".$intChangeUID.", ".$intUID.", 1, ".$intOldCredit.", ".$intNewCredit.", NOW(), '".$_SERVER["REMOTE_ADDR"]."')";
		//print $strSQL;
		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Update Credit Data Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($_ConnMaster).",\nLinue:".__LINE__.",\nPage:".__FILE__;
			AddData_Log($intChangeUID, "更改使用者信用額度錯誤", $GLOBALS["errorhidden"].$strSQL);
			return false;
		}
		//更改信用額度
		$strSQL = "update `bet_Member` set CreditLine = ".$intNewCredit." where ID  = ".$intUID;
		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Update Commission Data Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($_ConnMaster).",\nLinue:".__LINE__.",\nPage:".__FILE__;
			AddData_Log($intChangeUID, "更改使用者信用額度錯誤", $GLOBALS["errorhidden"].$strSQL);
			return false;
		}
		if(empty($strChildUID))return true;
		//取出子層信用額度總和
		$intSumCredit = getChildSumCreditLine($intUID, "", $intUserStatus);
		if($intSumCredit === false)return false;
		if($intSumCredit <= $intNewCredit)return true;
		//假如子層信用額度總和大於，使用者新的信用額度，其子層信用額度依使用者欲改之信用額度等比例下降
		$flRadio = ($intSumCredit < $intOldCredit)?($intNewCredit / $intOldCredit):($intNewCredit / $intSumCredit);

		$strSQL = "insert `bet_RateChangeRecord`(ChangeUserID, UserID, LogType, Scale, NewScale, ChangeDate, IP) select ".$intChangeUID.", ID, 1, CreditLine, FLOOR(".$flRadio."*CreditLine), NOW(), '".$_SERVER["REMOTE_ADDR"]."'  from `bet_Member` where ID in (".$strChildUID.") and isLock = '".$intUserStatus."'";
		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Update Commission Data Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($_ConnMaster).",\nLinue:".__LINE__.",\nPage:".__FILE__;
			AddData_Log($intChangeUID, "更改使用者信用額度錯誤", $GLOBALS["errorhidden"].$strSQL);
			return false;
		}

		$strSQL = "update `bet_Member` set CreditLine = FLOOR(".$flRadio."*CreditLine) where ID in (".$strChildUID.") and isLock = '".$intUserStatus."'";
		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Update Commission Data Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($_ConnMaster).",\nLinue:".__LINE__.",\nPage:".__FILE__;
			AddData_Log($intChangeUID, "更改使用者信用額度錯誤", $GLOBALS["errorhidden"].$strSQL);
			return false;
		}
		return true;
	}


/**
 * 依照球類，取得退水參數陣列
 * @param	intLType 號碼種類
 * @return	Array()
 */
	function getCommParamsArray($intLType){
		global $_Conn, $$_Conn;
		
		$strSQL = "select ID, Commission_Jump, A_Commission_MinValue as Aline, B_Commission_MinValue as Bline, C_Commission_MinValue as Cline from `bet_BetType` where LType = ".$intLType;
		//print $strSQL;
		if(!$RS = @PMA_mysql_query($strSQL, $$_Conn)){
			$GLOBALS["errormessage"] = "Find Params Data Error!!";
			$GLOBALS["errorhidden"] = "\n Error: ".PMA_mysql_error($$_Conn).",\nSQL: ".$strSQL.",\nLine: ".__LINE__.",\nPage: ".__FILE__;
			return false;
		}
		if(PMA_mysql_num_rows($RS) == 0){
			$GLOBALS["errormessage"] = "Can't Find Params Data!!";
			$GLOBALS["errorhidden"] = "\nSQL: ".$strSQL.",\nLine: ".__LINE__.",\nPage: ".__FILE__;
			return false;
		}
		$arrCP = array();
		while($Rows = PMA_mysql_fetch_object($RS)){
			$arrCP[0][$Rows -> ID] = $Rows -> Commission_Jump;
			$arrCP[1][$Rows -> ID] = $Rows -> Aline;
			$arrCP[2][$Rows -> ID] = $Rows -> Bline;
			$arrCP[3][$Rows -> ID] = $Rows -> Cline;
		}
		return $arrCP;
	}


/**
 * 取得使用者下層所有使用者序號
 * @param	strUID 使用者序號
 * @return	成功回傳 Array()，失敗回傳 false
 */
	function getAllChildUserID($strUID){
		global $_Conn, $$_Conn;
		if(empty($strUID))return array();

		/*if($strUID == -1){
			$strSQL = "select `ID` from `bet_Member` where `MemberGroup` = 5";
		}else{
			$strSQL = "select `ID` from `bet_Member` where `ParentID` in (".$strUID.")";
		}*/
		$strSQL = "select ID from `bet_Member` where ParentID in (".$strUID.")";
		//print $strSQL."<br>\n";
		if(!$RS = PMA_mysql_query($strSQL, $$_Conn)){
			$GLOBALS["errormessage"] = "Find Member Data Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($$_Conn).",\nLinue:".__LINE__.",\nPage:".__FILE__;
			return false;
		}
		if(PMA_mysql_num_rows($RS) == 0)return array();
		while($Rows = PMA_mysql_fetch_object($RS)){
			$arrID[] = $Rows -> ID;
		}
		return array_merge($arrID, getAllChildUserID(implode(",", $arrID)));
	}


/**
 * 取得使用者最底層所有使用者序號
 * @param	intUID 使用者序號
 * @param	intGroup 使用者群組  若 intGroup = 0 則找出所有底層序號
 * @return	成功回傳 Array()，失敗回傳 false
 */
	function getAllBaseUserID($intUID, $intGroup){
		global $_Conn, $$_Conn;
		$arrID = array();
		$arrGroup = array(2 => 'SubID', 3 => 'MainID', 4 => 'StockID', 5 => 'DirectorID');
		$strSQL = "select ID from `bet_Member` where MemberGroup = 1";
		if($intGroup != 0){
			$strGroupID = $arrGroup[$intGroup];
			if($strGroupID == null || $strGroupID == "")return $arrID;
			$strSQL .= " and ".$strGroupID." in (".$intUID.")";
		}
		
		//print $strSQL."<br>\n";
		if(!$RS = PMA_mysql_query($strSQL, $$_Conn)){
			$GLOBALS["errormessage"] = "Find Member Data Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($$_Conn).",\nLinue:".__LINE__.",\nPage:".__FILE__;
			return false;
		}

		if(PMA_mysql_num_rows($RS) == 0)return $arrID;
		while($Rows = PMA_mysql_fetch_object($RS)){
			$arrID[] = $Rows -> ID;
		}
		return $arrID;
	}


/**
 * 更改使用者或其下階層退水值
 * @param	intChangeUID 更改者序號
 * @param	strUID 使用者序號
 * @param	intLID 樂透序號
 * @param	intBT 下注種類
 * @param	intGLine 分盤種類
 * @param	flComm 退水值
 * @param	isSelf 是否使用者本身
 * @return	成功 true, 失敗 false
 */
	function setUserCommission($intChangeUID, $strUID, $intLID, $intBT, $intGLine, $flComm, $isSelf){
		global $_ConnMaster;

		if($flComm > 100)$flComm = 100;
		if($isSelf){
			$strItem = " and ROUND(Commission*100) != '".round($flComm*100)."'";
		}else{
			$strItem = " and ROUND(Commission*100) < '".round($flComm*100)."'";
		}
		$strSQL = "insert `bet_RateChangeRecord`(ChangeUserID, UserID, LogType, LottoID, Bet_Type, Scale, NewScale, ChangeDate, IP) select ".$intChangeUID.", MemID as UserID, Gamble_Type + 20, LottoID, Bet_Type, Commission, '".$flComm."', NOW(), '".$_SERVER["REMOTE_ADDR"]."'  from `bet_Commission` where MemID in (".$strUID.") and Bet_Type = ".$intBT." and LottoID = ".$intLID." and Gamble_Type = ".$intGLine.$strItem;
		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Update Commission Data Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($_ConnMaster).",\nLinue:".__LINE__.",\nPage:".__FILE__;
			AddData_Log($intChangeUID, "更改使用者退水值錯誤", $GLOBALS["errorhidden"].$strSQL);
			return false;
		}

		$strSQL = "update `bet_Commission` set Commission = ".$flComm." where MemID in (".$strUID.") and Bet_Type = ".$intBT." and LottoID = ".$intLID." and Gamble_Type = ".$intGLine.$strItem;
		//print $strSQL."<br>\n";
		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Update Commission Data Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($_ConnMaster).",\nLinue:".__LINE__.",\nPage:".__FILE__;
			AddData_Log($intChangeUID, "更改使用者退水值錯誤", $GLOBALS["errorhidden"].$strSQL);
			return false;
		}

		//更改退水轉賠率
		$flGap = 100 - $flComm;
		$strSQL = "update `bet_Commission` set Commission = ".$flGap." where MemID in (".$strUID.") and Bet_Type = ".$intBT." and LottoID = ".$intLID." and Gamble_Type = 11 and Commission > ".$flGap;
		//print $strSQL."<br>\n";
		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Update CommToOdds Data Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($_ConnMaster).",\nLinue:".__LINE__.",\nPage:".__FILE__;
			AddData_Log($intChangeUID, "更改使用者退水轉賠率值錯誤", $GLOBALS["errorhidden"].$strSQL);
			return false;
		}
		return true;
	}


/**
 * 更改使用者或其下階層限額值
 * @param	intChangeUID 更改者序號
 * @param	strUID 使用者序號
 * @param	intLID 樂透序號
 * @param	intBT 下注種類
 * @param	intLimitType 限額種類
 * @param	flLimit 限額值
 * @param	isSelf 是否使用者本身
 * @return	成功 true, 失敗 false
 */
	function setUserLimitValue($intChangeUID, $strUID, $intLID, $intBT, $intLimitType, $flLimit, $isSelf){
		global $_ConnMaster;

		if($isSelf){
			$strItem = " and LimitValue != ".floor($flLimit);
		}else{
			$strItem = " and LimitValue > ".floor($flLimit);
		}
		$strSQL = "insert `bet_RateChangeRecord`(ChangeUserID, UserID, LogType, LottoID, Bet_Type, Scale, NewScale, ChangeDate, IP) select ".$intChangeUID.", MemID as UserID, LimitType + 10, LottoID, Bet_Type, LimitValue, '".$flLimit."', NOW(), '".$_SERVER["REMOTE_ADDR"]."'  from `bet_MemberLimit` where MemID in (".$strUID.") and Bet_Type = ".$intBT." and LottoID = ".$intLID." and LimitType = ".$intLimitType.$strItem;
		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Update Limit Data Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($_ConnMaster).",\nLinue:".__LINE__.",\nPage:".__FILE__;
			AddData_Log($intChangeUID, "更改使用者限額錯誤", $GLOBALS["errorhidden"].$strSQL);
			return false;
		}

		$strSQL = "update `bet_MemberLimit` set LimitValue = '".$flLimit."' where MemID in (".$strUID.") and Bet_Type = ".$intBT." and LottoID = ".$intLID." and LimitType = ".$intLimitType.$strItem;
		//print $strSQL;
		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Update Limit Data Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($_ConnMaster).",\nLinue:".__LINE__.",\nPage:".__FILE__;
			AddData_Log($intChangeUID, "更改使用者限額錯誤", $GLOBALS["errorhidden"].$strSQL);
			return false;
		}
		return true;
	}

/**
 * 取得使用者其父層資料
 * @param	intUID 使用者序號
 * @return	成功 Array(), 失敗 false
 */
	function getParentData($intUID){
		global $_Conn, $$_Conn;
		$strSQL = "select a.isLock as isLock, a.ID as ID, b.ExchangeRate as SelfECRate, a.CreditLine as CreditLine, a.Rate1 as Rate1, a.Rate2 as Rate2 from `bet_Member` a, `bet_Member` b where a.ID = b.ParentID and b.ID = ".(int)$intUID;
		//print $strSQL;
		if(!$RS = PMA_mysql_query($strSQL, $$_Conn)){
			$GLOBALS["errormessage"] = "Find Member Data Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($$_Conn).",\nLinue:".__LINE__.",\nPage:".__FILE__;
			return false;
		}

		if(PMA_mysql_num_rows($RS) == 0)return array();
		return PMA_mysql_fetch_array($RS, MYSQL_ASSOC);
	}


/**
 * 依使用者序號取得使用者其資料陣列
 * @param	intUID 使用者序號
 * @return	成功 Array, 失敗 false
 */
	function getUserDataArrayByID($intUID){
		global $_Conn, $$_Conn;
		$strSQL = "select * from `bet_Member` where ID = ".$intUID;
		if(!$RS = @PMA_mysql_query($strSQL, $$_Conn)){
			$GLOBALS["errormessage"] = "Find Member Data Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($$_Conn).",\nLinue:".__LINE__.",\nPage:".__FILE__;
			return false;
		}
		
		if(PMA_mysql_num_rows($RS) == 0)return array();
		return PMA_mysql_fetch_array($RS, MYSQL_ASSOC);
	}


/**
 * 依使用者帳號取得使用者其資料陣列
 * @param	strAccount 使用者帳號
 * @return	成功 Array, 失敗 false
 */
	function getUserDataArrayByAccount($strAccount){
		global $_Conn, $$_Conn;
		$strSQL = "select * from `bet_Member` where Account = '".$strAccount."'";
		if(!$RS = @PMA_mysql_query($strSQL, $$_Conn)){
			$GLOBALS["errormessage"] = "Find Member Data Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($$_Conn).",\nLinue:".__LINE__.",\nPage:".__FILE__;
			return false;
		}
		
		if(PMA_mysql_num_rows($RS) == 0)return array();
		return PMA_mysql_fetch_array($RS, MYSQL_ASSOC);
	}


/**
 * 登出使用者
 * @param	strUserID 使用者序號
 * @return	成功 true, 失敗 false
 */
	function KickOutUser($strUserID){
		if(!UserSession_getOut($strUserID))return false;
		return true;
	}


/**
 * 更改使用者資料
 * @param	intChangeUID 更改者序號
 * @param	strUserID 使用者序號
 * @param	strGroup 使用者群組
 * @param	arrData 使用者資料陣列
 * @return	成功 true, 失敗 false
 */
	function setUserData($intChangeUID, $strUserID, $strGroup, $arrData){
		global $_ConnMaster;
		$arrSet = array();

		if(!is_array($arrData))return false;
		foreach($arrData as $key => $val){
			$arrSet[] = $key." = '".$val."'";
		}
		if(sizeof($arrSet) == 0)return false;
		$strSQL = "update `bet_Member` set ".implode(", ", $arrSet)." where ID in (".$strUserID.")";
		if($strGroup > 0)$strSQL .= " and MemberGroup in (".$strGroup.")";
		//print $strSQL;
		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Update User Data Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($_ConnMaster).",\nLinue:".__LINE__.",\nPage:".__FILE__;
			AddData_Log($intChangeUID, "更改使用者資料錯誤", $GLOBALS["errorhidden"].$strSQL);
			return false;
		}
		AddData_Log($intChangeUID, "更改使用者資料完成", "ID：".$strUserID."\n".print_r($arrData, true));
		return true;
	}


/**
 * 停用該使用者其下階層帳號
 * @param	intChangeUID 更改者序號
 * @param	strChildUID 使用者序號
 * @param	isLock 使否停用或啟用
 * @param	strLockTime 停用時間
 * @return	成功 true, 失敗 false
 */
	function setLockAllChild($intChangeUID, $strChildUID, $isLock, $strLockTime){
		global $_ConnMaster;
		if((int)$isLock == 0){
			$strSQL = "update `bet_Member` set isLock = '0' where ID in (".$strChildUID.") and isLock in ('1', '2') and LockTime = '".$strLockTime."'";
		}else{
			if((string)$isLock != '1')$isLock = '2';
			$strSQL = "update `bet_Member` set isLock = '".$isLock."', LockTime = '".$strLockTime."' where ID in (".$strChildUID.") and isLock = '0'";
		}
		//print $strSQL;
		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Update User Lock Data Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($_ConnMaster).",\nLinue:".__LINE__.",\nPage:".__FILE__;
			AddData_Log($intChangeUID, "停用使用者錯誤", $GLOBALS["errorhidden"].$strSQL);
			return false;
		}
		AddData_Log($intChangeUID, "停用使用者完成", "ID：".$strChildUID."\nisLock：".$isLock."\nLockTime：".$strLockTime);
		return true;
	}


/**
 * 停用該使用者其下階層帳號之代理下注功能
 * @param	intChangeUID 更改者序號
 * @param	strChildUID 使用者序號
 * @return	成功 true, 失敗 false
 */
	function setStopAgencyWagerAllChild($intChangeUID, $strChildUID){
		global $_ConnMaster;
		$strSQL = "update `bet_Member` set isAgency = '1' where ID in (".$strChildUID.")";

		if(!@PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Update User Agency Wager Data Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($_ConnMaster).",\nLinue:".__LINE__.",\nPage:".__FILE__;
			AddData_Log($intChangeUID, "停用使用者代理下注錯誤", $GLOBALS["errorhidden"].$strSQL);
			return false;
		}
		AddData_Log($intChangeUID, "停用使用者代理下注完成", "ID：".$strChildUID);
		return true;
	}


/**
 * 增加使用者的單場、單注限額
 * @param	intPUID 使用者父層序號
 * @param	intUID 使用者序號
 * @return	成功 true, 失敗 false
 */
	function setUserAllLimitsFormParent($intPUID, $intUID){
		global $_ConnMaster;
		$strSQL = "insert into `bet_MemberLimit`(MemID, LottoID, Bet_Type, LimitType, LimitValue) select ".$intUID.", LottoID, Bet_Type, LimitType, LimitValue from `bet_MemberLimit` where MemID = '".$intPUID."' and LimitType in ('0','1')";
		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Set User Limit Data Error!!";
			$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($_ConnMaster)."\nSQL:".$strSQL."\nLine:".__LINE__."\nPage:".__FILE__;
			AddData_Log("", "增加使用者限額錯誤", $GLOBALS["errorhidden"]);
			return false;
		}
		return true;
	}

/**
 * 更改使用者的單場、單注限額
 * @param	intUID 使用者序號
 * @param	arrLimits 限額陣列
 * @return	成功 true, 失敗 false
 */
	function setUserAllLimits($intUID, $arrLimits){
		global $_ConnMaster;
		$strLog = "\nUserID = ".$intUID."： \n";
		if(!is_array($arrLimits)){
			$GLOBALS["errormessage"] = "User Limit Data 1 is not Array!!";
			$GLOBALS["errorhidden"] = "Line:".__LINE__."\nPage:".__FILE__."\nFile: ".$_SERVER["PHP_SELF"];
			return false;
		}

		foreach($arrLimits as $key => $arrLimits2){
			$intLT = (int)$key;

			if(!is_array($arrLimits2)){
				$GLOBALS["errormessage"] = "User Limit Data 3 is not Array!!";
				$GLOBALS["errorhidden"] = "Line:".__LINE__."\nPage:".__FILE__;
				return false;
			}

			foreach($arrLimits2 as $key => $arrLimits3){
				$intLID = (int)$key;

				if(!is_array($arrLimits3)){
					$GLOBALS["errormessage"] = "User Limit Data 3 is not Array!!";
					$GLOBALS["errorhidden"] = "Line:".__LINE__."\nPage:".__FILE__;
					return false;
				}

				foreach($arrLimits3 as $key => $val){
					$intBT = (int)$key;
					$intLV = (int)$val;

					$strLog .= "LottoID = '".$intLID."', Bet_Type = ".$intBT.", LimitType = ".$intLT.",  LimitValue = ".$intLV."\n";

					$strSQL = "insert into `bet_MemberLimit`(`MemID`, `LottoID`, `Bet_Type`, `LimitType`, `LimitValue`) values('".$intUID."', '".$intLID."','".$intBT."','".$intLT."','".$intLV."') ON DUPLICATE KEY UPDATE `LimitValue` = '".$intLV."'";
					if(!PMA_mysql_query($strSQL, $_ConnMaster)){
						$GLOBALS["errormessage"] = "Add User Limit Data Error!!";
						$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($_ConnMaster)."\nSQL:".$strSQL."\nLine:".__LINE__."\nPage:".__FILE__;
						AddData_Log("", "更改使用者限額錯誤", $strLog."\n".$GLOBALS["errorhidden"]);
						return false;
					}
				} // foreach($arrLimits3 as $key => $val) //
			} // foreach($arrLimits as $key => $arrLimits2) //
		} // foreach($arrLimits as $key => $arrLimits2) //
		AddData_Log("", "更改使用者限額完成", $strLog);
		return true;
	}


/**
 * 增加使用者的退水值
 * @param	intPUID 使用者父層序號
 * @param	intUID 使用者序號
 * @param	intGLine 分盤種類
 * @param	isMax 分盤是否退到底
 * @return	成功 true, 失敗 false
 */
	function setUserAllCommissionFormParent($intPUID, $intUID, $intGLine, $isMax){
		global $_ConnMaster;
		if($isMax == 1){
			$strSQL = "insert into `bet_Commission`(MemID, LottoID, Bet_Type, Gamble_Type, Commission) select ".$intUID.", LottoID, Bet_Type, Gamble_Type, Commission from `bet_Commission` where MemID = '".$intPUID."'";
		}else{
			$strSQL = "insert into `bet_Commission`(MemID, LottoID, Bet_Type, Gamble_Type, Commission) select ".$intUID.", LottoID, Bet_Type, Gamble_Type, 100 from `bet_Commission` where MemID = '".$intPUID."'";
		}
		if(!empty($intGLine))$strSQL .= " and Gamble_Type = ".$intGLine;
		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Set User Commission Data Error!!";
			$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($_ConnMaster)."\nSQL:".$strSQL."\nLine:".__LINE__."\nPage:".__FILE__;
			AddData_Log("", "增加使用者退水值錯誤", $GLOBALS["errorhidden"]);
			return false;
		}
		return true;
	}

/**
 * 更改使用者的退水值
 * @param	intUID 使用者序號
 * @param	arrComm 退水值陣列
 * @return	成功 true, 失敗 false
 */
	function setUserAllCommission($intUID, $arrComm){
		global $_ConnMaster;
		$strLog = "\nMemID = ".$intUID."： \n";

		if(!is_array($arrComm)){
			$GLOBALS["errormessage"] = "Commission Data 1 is not Array!!";
			$GLOBALS["errorhidden"] = "Line:".__LINE__."\nPage:".__FILE__."\nFile: ".$_SERVER["PHP_SELF"];
			return false;
		}

		foreach($arrComm as $key => $arrComm2){
			$intLID = (int)$key;

			if(!is_array($arrComm2)){
				$GLOBALS["errormessage"] = "Commission Data 2 is not Array!!";
				$GLOBALS["errorhidden"] = "Line:".__LINE__."\nPage:".__FILE__;
				return false;
			}
			foreach($arrComm2 as $key => $arrComm3){
				$intBT = (int)$key;

				if(!is_array($arrComm3)){
					$GLOBALS["errormessage"] = "Commission Data 3 is not Array!!";
					$GLOBALS["errorhidden"] = "Line:".__LINE__."\nPage:".__FILE__;
					return false;
				}

				foreach($arrComm3 as $key => $val){
					$intGT = (int)$key;
					$flComm = (float)$val;

					$strLog .= "LottoID = '".$intLID."', Bet_Type = '".$intBT."', Gamble_Type = '".$intGT."',  Commission = ".$flComm."\n";

					$strSQL = "insert into `bet_Commission`(`MemID`, `LottoID`, `Bet_Type`, `Gamble_Type`, `Commission`) values('".$intUID."', '".$intLID."','".$intBT."','".$intGT."','".$flComm."') ON DUPLICATE KEY UPDATE `Commission` = '".$flComm."'";
					if(!PMA_mysql_query($strSQL, $_ConnMaster)){
						$GLOBALS["errormessage"] = "Add Commission Data Error!!";
						$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($_ConnMaster)."\nSQL:".$strSQL."\nLine:".__LINE__."\nPage:".__FILE__;
						AddData_Log("", "更改使用者退水值錯誤", $strLog."\n".$GLOBALS["errorhidden"]);
						return false;
					}
				}  //  foreach($arrComm3 as $key => $val)  //
			}  //  foreach($arrComm2 as $key => $arrComm3)  //
		}	//	foreach($arrComm as $key => $arrComm2) //
		AddData_Log("", "更改使用者退水值完成", $strLog);
		return true;
	}


/**
 * 更改使用者單場總額
 * @param	intChangeUID 更改者序號
 * @param	strUID 使用者序號
 * @param	intLID 號碼序號
 * @param	intBT 下注種類
 * @param	flLimit 限額值
 * @return	成功 true, 失敗 false
 */
	function setUserAcceptLimitValue($intChangeUID, $intUID, $intLID, $intBT, $flLimit){
		global $_ConnMaster;
		if(!($intLID == 4 && $intBT >= 7 && $intBT <= 11))return true;
		$intLType = 2;
		$strSQL = "select ID from `bet_MemberLimit` where MemID in (".$intUID.") and Bet_Type = ".$intBT." and LottoID = ".$intLID." and LimitType = ".$intLType;
		if(!$RS = PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Find Limit Data Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($_ConnMaster).",\nLinue:".__LINE__.",\nPage:".__FILE__;
			return false;
		}
		//print $strSQL;
		if(PMA_mysql_num_rows($RS) > 0)return setUserLimitValue($intChangeUID, $intUID, $intLID, $intBT, $intLType, $flLimit, true);

		$strSQL = "insert into `bet_RateChangeRecord`(ChangeUserID, UserID, LottoID, LogType, Bet_Type, Scale, NewScale, ChangeDate, IP) values( ".$intChangeUID.", ".$intUID.", ".$intLID.", ".($intLType + 10).", ".$intBT.", 0, '".$flLimit."', NOW(), '".$_SERVER["REMOTE_ADDR"]."')";
		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Update Limit Data Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($_ConnMaster).",\nLinue:".__LINE__.",\nPage:".__FILE__;
			AddData_Log($intChangeUID, "更改使用者單場總額錯誤", $GLOBALS["errorhidden"].$strSQL);
			return false;
		}

		$strSQL = "insert into `bet_MemberLimit`(MemID, LottoID, Bet_Type, LimitType, LimitValue) values(".$intUID.", ".$intLID.", ".$intBT.", ".$intLType.", '".$flLimit."')";
		//print $strSQL;
		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Update Limit Data Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($_ConnMaster).",\nLinue:".__LINE__.",\nPage:".__FILE__;
			AddData_Log($intChangeUID, "更改使用者單場總額錯誤", $GLOBALS["errorhidden"].$strSQL);
			return false;
		}
		return true;
	}


/**
 * 更改使用者自動補貨金額上限
 * @param	intChangeUID 更改者序號
 * @param	strUID 使用者序號
 * @param	intLID 號碼序號
 * @param	intBT 下注種類
 * @param	flLimit 限額值
 * @return	成功 true, 失敗 false
 */
	function setUserAuotSuppleLimitValue($intChangeUID, $intUID, $intLID, $intBT, $flLimit){
		global $_ConnMaster;

		$intLType = 3;
		$strSQL = "select ID from `bet_MemberLimit` where MemID in (".$intUID.") and Bet_Type = ".$intBT." and LottoID = ".$intLID." and LimitType = ".$intLType;
		if(!$RS = PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Find Limit Data Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($_ConnMaster).",\nLinue:".__LINE__.",\nPage:".__FILE__;
			return false;
		}
		//print $strSQL;
		if(PMA_mysql_num_rows($RS) > 0)return setUserLimitValue($intChangeUID, $intUID, $intLID, $intBT, $intLType, $flLimit, true);

		$strSQL = "insert into `bet_RateChangeRecord`(ChangeUserID, UserID, LottoID, LogType, Bet_Type, Scale, NewScale, ChangeDate, IP) values( ".$intChangeUID.", ".$intUID.", ".$intLID.", ".($intLType + 10).", ".$intBT.", 0, '".$flLimit."', NOW(), '".$_SERVER["REMOTE_ADDR"]."')";
		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Update Limit Data Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($_ConnMaster).",\nLinue:".__LINE__.",\nPage:".__FILE__;
			AddData_Log($intChangeUID, "更改使用者單場總額錯誤", $GLOBALS["errorhidden"].$strSQL);
			return false;
		}

		$strSQL = "insert into `bet_MemberLimit`(MemID, LottoID, Bet_Type, LimitType, LimitValue) values(".$intUID.", ".$intLID.", ".$intBT.", ".$intLType.", '".$flLimit."')";
		//print $strSQL;
		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Update Limit Data Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($_ConnMaster).",\nLinue:".__LINE__.",\nPage:".__FILE__;
			AddData_Log($intChangeUID, "更改使用者單場總額錯誤", $GLOBALS["errorhidden"].$strSQL);
			return false;
		}
		return true;
	}


/**
 * 產生使用者群組下拉清單
 * @param	intGroupID 指定群組序號
 * @param	strGroup 去取得群組序號集合
 * @param	strFieldName select tag 的name
 * @param	onChange onchange 時，要執行的事件
 * @return	成功傳回 String
 */
	function getUserGroupSelectTag($intGroupID, $strGroup, $strFieldName, $onChange){
		global $_Conn, $$_Conn, $_intLang;
		$strSQL = "select ID, GroupName_".$_intLang." as GroupName from `bet_MemberGroup` where ID in (".$strGroup.")";
		//print $strSQL;
		if(empty($onChange))$onChange="void(0)";

		$strCon = "<select name=\"".$strFieldName."\" onChange=\"".$onChange."\">\n";
		$strCon .= "<option value=\"\">--select--</option>\n";
		if($RS = @PMA_mysql_query($strSQL, $$_Conn)){
			while($Rows = PMA_mysql_fetch_object($RS)){
				$strCon .= "<option value=\"".$Rows -> ID."\"".(($intGroupID == $Rows -> ID)?" selected":"").">".$Rows -> GroupName."</option>\n";
			}
		}
		$strCon .= "</select>";
		return $strCon;
	}


/**
 * 取得修改成數 Select 下拉選單
 * 系統參數
 * _KSysParam[CompanyMinScale]：公司最少必要成數
 * @param	strField 下拉選單欄位名稱
 * @param	intRate 以選擇織成數值
 * @param	strChange 當 Select OnChange 時，必須執行的 JavaScript 指令 
 * @param	intMinScale 額外最少扣除之成數(股東成數)
 * @param	intMinSelectScale 可選擇成數下限
 * @return	成功傳回 String
 */
	function SelectRate($strField, $intRate, $strChange, $intMinScale, $intMinSelectScale){
		global $_KSysParam;
		$intMax = round(100 - ($_KSysParam[CompanyMinScale] + $intMinScale));
		$strCon = "<select name=\"".$strField."\" Onchange=\"".$strChange."\">\n";
		for($i = $intMax; $i >= $intMinSelectScale; $i -= 5){
			if($i == (int)$intRate){
				$strCon .= "<option value=\"".$i."\" selected> ".$i." %</option>\n";
			}else{
				$strCon .= "<option value=\"".$i."\"> ".$i." %</option>\n";
			}
		}
		$strCon .= "</select>\n";
		return $strCon;
	}  //  function SelectRate()  //


	/**
	 * 取得 使用者成數 select Tag
	 * @param	intUserScale 使用者成數
	 * @param	intMaxScale 最大成數
	 * @return	false -> 錯誤, string -> 成功, 
	 */
	function getUserScaleSelectTag($intUserScale, $intMaxScale){
		if($intMaxScale > 100)$intMaxScale = 100;
		if($intMaxScale < 0)$intMaxScale = 0;
		

		$strOption = "";
		$intNo = 0;
		for($intScale = 0; $intScale <= $intMaxScale; $intScale += 5){
			if($intNo > 30)break;

			$strSelected = ($intScale == $intUserScale)?" selected":"";
			$strOption .= "<option value='".$intScale."'".$strSelected.">".$intScale." %</option>\n";
			$intNo++;
		}
		return $strOption;
	}


/**
 * 退水質設定是否超過父層
 * @param	intPID 父層序號
 * @param	intUID 使用者序號
 * @return	1 大於, 0 未超過, false 錯誤
 */
	function isOverParentCommission($intPID, $intUID){
		global $_Conn, $$_Conn;
		$strSQL = "select * from `bet_Commission` where MemID in (".$intPID.", ".$intUID.")";
		if(!$RS = mysql_query($strSQL, $$_Conn)){
			$GLOBALS["errormessage"] = "Find Commission Data Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".mysql_error($$_Conn).",\nLinue:".__LINE__.",\nPage:".__FILE__;
			return false;
		}
		$arrComm[$intPID] = array();
		$arrComm[$intUID] = array();
		while($Rows = mysql_fetch_object($RS)){
			$arrComm[$Rows -> MemID][$Rows -> LottoID."-".$Rows -> Bet_Type."-".$Rows -> Gamble_Type] = $Rows -> Commission;
		}

		if(sizeof($arrComm[$intPID]) == 0 || sizeof($arrComm[$intUID]) == 0){
			$GLOBALS["errormessage"] = "Find No Commission Data!!";
			$GLOBALS["errorhidden"] = "\nError: ".mysql_error($$_Conn).",\nLinue:".__LINE__.",\nPage:".__FILE__.",\n".print_r($arrComm, true);
			return false;
		}

		foreach($arrComm[$intUID] as $strKey => $flComm){
			if($flComm < $arrComm[$intPID][$strKey] || !isset($arrComm[$intPID][$strKey])){
				$GLOBALS["errorhidden"] = "\nError: ".mysql_error($$_Conn).",\nLinue:".__LINE__.",\nPage:".__FILE__.",\n".$strKey.",\n P:".$arrComm[$intPID][$strKey].",\nC:".$flComm;
				return 1;
			}
		}
		return 0;
	}


/**
 * 取得使用者以使用信用額度
 * @param	intUserID 使用者序號
 * @param	intGroup 使用者群組
 * @return	成功傳回 int
 */
	function getUserUsedCredit($intUserID, $intGroup){
		global $_Conn, $$_Conn;
		$arrGroup = array(1 => "MemID", 2 => "SubID", 3 => "MainID", 4 => "StockID",  5 => "DirectorID");
		$strSQL = "select SUM((IF(MemResult = 0, MemStake, MemResult*-1) - MemStake*MemCommission)*MemExchangeRate) as sumUsedCredit from `bet_BetListTmp` where ".$arrGroup[$intGroup]." = '".$intUserID."' and PayupDate >= CURRENT_DATE() ";

		if(!$RS = @PMA_mysql_query($strSQL, $$_Conn)){
			$GLOBALS["errormessage"] = "Find Member Data Error!!";
			$GLOBALS["errorhidden"] = "\n Error: ".PMA_mysql_error($$_Conn).",\nSQL: ".$strSQL.",\nLine: ".__LINE__.",\nPage: ".__FILE__;
			return false;
		}

		if(PMA_mysql_num_rows($RS) == 0)return 0;
        $Rows = @PMA_mysql_fetch_object($RS);
		return $Rows -> sumUsedCredit;
	}

/**
 * 取得多筆使用者資料
 * @param	strUID 使用者序號
 * @return	成功傳回 Array
 */
	function getMultiUserAccNameByID($strUID){
		global $_Conn, $$_Conn;

		if($strUID == "")return array();

		$arrUser = array();
		$strSQL = "select * from `bet_Member` where ID in (".$strUID.")";
		if(!$RS = @PMA_mysql_query($strSQL, $$_Conn)){
			$GLOBALS["errormessage"] = "Find User Data Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($$_Conn).",\nLinue:".__LINE__.",\nPage:".__FILE__;
			ErrMsgOutputHTML("LoginError", "", 1);
		}

        while($objRows = PMA_mysql_fetch_object($RS)){
			$arrUser[$objRows -> ID] = $objRows;
		}
		return $arrUser;
	}

/**
 * 取得認證碼
 * @return	成功傳回 string
 */
	function getAuthCode(){
		mt_srand((double)microtime()*100000000);
		return sprintf("%06s", mt_rand(0, 999999));
	}

/**
 * 設置使用者認證
 * @param	intUID 使用者序號
 * @param	strType 認證種類
 * @param	strAuthCode 認證碼
 * @return	成功傳回 true
 */
	function setUserAuthCode($intUID, $strType, $strAuthCode){
		global $_ConnMaster;

		$strSQL = "insert into `bet_Authentication`(`UserID`, `MType`, `ACode`, `isCheck`, `ADate`, `ATime`) values('".$intUID."', '".$strType."', '".$strAuthCode."', '0', CURRENT_DATE(), CURRENT_TIME()) ON DUPLICATE KEY UPDATE `ACode` = VALUES(ACode), `isCheck` = 0, `ADate` = CURRENT_DATE(), `ATime` = CURRENT_TIME()";
		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Update Data Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($_ConnMaster).",\nLinue:".__LINE__.",\nPage:".__FILE__;
			return false;
		}
		return true;
	}

/**
 * 核對使用者認證
 * @param	intUID 使用者序號
 * @param	strType 認證種類
 * @param	strAuthCode 認證碼
 * @return	成功傳回 true
 */
	function checkUserAuthCode($intUID, $strType, $strAuthCode){
		global $_ConnMaster;

		$strSQL = "update `bet_Authentication` set `isCheck` = 1 where `UserID` = '".$intUID."' and `MType` = '".$strType."' and `ACode` = '".$strAuthCode."'";
		//print $strSQL;
		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Update Data Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($_ConnMaster).",\nLinue:".__LINE__.",\nPage:".__FILE__;
			return false;
		}
		if(mysql_affected_rows($_ConnMaster) == 0)return 0;
		return true;
	}

/**
 * 發送使用者認證
 * @param	strUserAcc 使用者序號
 * @param	strMType 認證種類
 * @param	strMData 聯絡資料
 * @param	strAuthCode 認證碼
 * @return	成功傳回 true
 */
	function sendUserAuthCode($strUserAcc, $strMType, $strMData, $strAuthCode){
		preg_match("/www\/([^\/]+)\/secure/", $_SERVER["DOCUMENT_ROOT"], $arrMatch);
		$strPath = $arrMatch[1];
		if($strPath == "")$strPath = "lottou";

		$strLink = "http://exmis.uuss.net/VIP/setverify.php?path=".urlencode($strPath)."&account=".$strUserAcc."&type=".$strMType."&commdata=".urlencode($strMData)."&authcode=".$strAuthCode;
		$arrFCon = file($strLink);
		$strFCon = implode("", $arrFCon);
		$strLog = "Link: ".$strLink.", \nReturn: ".$strFCon;

		AddData_Log(null, "發送認證碼", $strLog);
		if(trim($strFCon) != "OK"){
			$GLOBALS["errormessage"] = "Send AuthCode Data Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".$strFCon.",\nLinue:".__LINE__.",\nPage:".__FILE__;
			return false;
		}
		return true;
	}
//-------------------------------------------------------//
//End
}
?>